import { useMemo } from 'react';
import _ from 'lodash';
import moment from 'moment';
import { WithNormalRender } from "@ey-render/hoc-form";
// import { WithNormalRender } from '@/biz-components/hoc-form/with-normal';
import { getSubmitFormValues, getFormValues } from '@ey-utils/utils/formily-utils/getSubmitFormValues';
import { history } from 'umi';
import { uuidv4 } from '@ey-utils/utils/utils';
import { onFieldChange, onFieldInit, onFieldValueChange, onFormMount } from '@formily/core';
import { archivesCreate } from './services';
import components from "@/components/schema-form-render/schema-components-registed/components-biz";

const ArchivesMngCreate = () => {
  const handleBack = () => {
    return history.back();
  };

  const BillFormLayout = useMemo(() => {
    return WithNormalRender({
        filedConfig: {},
        components: {
            ...components,
        },
    });
  }, []);

  // 提交
  const submit = async ({ form }: any) => {
    const formValues = await getSubmitFormValues(form);
    if (formValues) {
      const params = {
        ...formValues,
        pageCode: 'ArcvManage_C'
      };
      const res: any = await archivesCreate(params);

      const { success } = res || {};

      if (success) {
        handleBack();
      }

      return res;
    }

    return {
      success: false,
    };
  };

  return (
    <BillFormLayout
      anchor
      type="NORMAL"
      pageCode="ArcvManage_C"
      events={{
        submit,
      }}
      formConfigs={{
        effects: () => {
            onFormMount((form) => {
                if (!form.getValuesIn('pageId')) {
                    form.setValuesIn('pageId', uuidv4());
                }
                // form.setFieldState('arcvType', (field) => {
                //     field.componentProps = {
                //         ...field.componentProps,
                //         filterData: [
                //           "Form",
                //           "Attachments",
                //           "Invoices",
                //           "ElecApvlRec",
                //           "BankElecReceipt"
                //         ]
                //     }
                // })
            })
            onFieldValueChange('qzCode', (field, form) => {
                form.setValuesIn('qzName', field.inputValues?.[1]?.name);
            })
            onFieldValueChange('type', (field, form) => {
              form.setFieldState('ctgryCode', state => {
                state.required = field.value === 'ReportForms';
              })
            })
        },
      }}
      filedConfig={{}}
    />
  );
};

export default ArchivesMngCreate;
